package com.example.mykotilin.room2

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.activity.viewModels
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.coroutineScope
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.mykotilin.MyApplication
import com.example.mykotilin.R
import com.example.mykotilin.databinding.ActivityBusScheduleBinding
import com.example.mykotilin.databinding.ActivityFilterBinding
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import kotlinx.coroutines.plus


class BusScheduleActivity : AppCompatActivity() {

    private val viewModel: BusScheduleViewModel by viewModels {
        BusScheduleViewModelFactory((application as MyApplication).database.scheduleDao())
    }

    lateinit var binding: ActivityBusScheduleBinding
    lateinit var recyclerView: RecyclerView
    lateinit var busStopAdapter: BusStopAdapter
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivityBusScheduleBinding.inflate(layoutInflater).apply {
            setContentView(root)
            bindViews(this)
        }


    }

    private fun bindViews(binding: ActivityBusScheduleBinding) {
        with(binding) {
            recyclerView = rc
            recyclerView.layoutManager = LinearLayoutManager(this@BusScheduleActivity)

            val _busStopAdapter = BusStopAdapter({})
            busStopAdapter = _busStopAdapter
            recyclerView.adapter = _busStopAdapter
            lifecycle.coroutineScope.launch {
                viewModel.funSchedule().collect {
                    busStopAdapter.submitList(it)
                }
            }
        }


    }

    override fun onDestroy() {
        super.onDestroy()

    }
}